package org.auto;

import org.apache.commons.math3.ml.clustering.Cluster;
import org.apache.commons.math3.ml.clustering.DBSCANClusterer;
import org.apache.commons.math3.ml.clustering.DoublePoint;
import org.apache.commons.math3.ml.distance.DistanceMeasure;
import org.apache.commons.math3.ml.distance.EuclideanDistance;

import java.util.ArrayList;
import java.util.List;

public class DBSCANExample {
    public static void main(String[] args) {
        // 创建数据点
        double[][] points = new double[][] {
                {3, 8},
                {4, 7},
                {4, 8},
                {5, 6},
                {3, 9},
                {5, 1},
                {5, 2},
                {6, 3},
                {7, 3},
                {7, 4},
                {0, 2},
                {8, 16},
                {1, 1},
                {1, 3}
        };
        List<DoublePoint> pointList = new ArrayList<>();
        for (double[] point : points) {
            pointList.add(new DoublePoint(point));
        }

        // 创建距离度量
        DistanceMeasure measure = new EuclideanDistance();

        // 创建并配置DBSCAN聚类器
        DBSCANClusterer<DoublePoint> clusterer = new DBSCANClusterer<>(2, 3, measure);

        // 执行聚类
        List<Cluster<DoublePoint>> clusterList = clusterer.cluster(pointList);
        // 获取簇
        System.out.println("Clusters found: " + clusterList.size());

        for(Cluster cl : clusterList) {
            System.out.println(cl.getPoints());
            // 获取噪声点
            System.out.println("Noise points: " + clusterer);
        }
    }
}